package com.aaa.custom.dao.cart;

import com.aaa.entity.cart_order.kwc.Cart;
import com.aaa.entity.cart_order.kwc.CartList;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * fileName:CartService
 * description:
 * author:xxx
 * createTime:2020/2/14 13:27
 * version:1.0.0
 */
public interface CartDao {

    /**
     * 根据用户Id及店铺Id查询购物车列表
     * @param member_id
     * @param shop_id
     * @return
     */
    @Select("SELECT id,shop_id,product_id,quantity,product_pic,product_name,price,member_id" +
            "  from oms_cart_item where member_id = #{member_id} and shop_id= #{shop_id} and delete_status = 0")
    @Results({
            @Result(property = "product",column = "product_id",
                    one = @One(select = "com.aaa.custom.dao.Products.ProductsDao.OneProductById")
            )
    })
    List<CartList> getCartById(@Param("member_id") long member_id,@Param("shop_id") long shop_id);


    /**
     * 清空购物车
     * @param member_id
     * @param shop_id
     * @return
     */
    @Update("update oms_cart_item set delete_status = 1,quantity = 0 where shop_id = #{shop_id}  and member_id = #{member_id}")
    int deleteAll(@Param("member_id") long member_id,@Param("shop_id") long shop_id);


    /**
     * 添加购物车
     * @param cart
     * @return
     */
    @Insert("insert into oms_cart_item (shop_id,member_id,quantity,product_pic,product_name,price,product_id,delete_status) values" +
            " (#{shop_id},#{member_id},#{quantity},#{product_pic},#{product_name},#{price},#{product.product_id},#{delete_status} )")
    int addCart(CartList cart);


    /**
     * 判断传入商品是否存在购物车
     * @param member_id
     * @param shop_id
     * @param product_id
     * @return
     */
    @Select("select * from oms_cart_item where shop_id = #{shop_id} and product_id=#{product_id} and member_id = #{member_id} ")
    List<CartList> ExisProduct(@Param("member_id") long member_id,@Param("shop_id") long shop_id,@Param("product_id") long product_id);


    /**
     * 相同商品数量加一
     * @param member_id
     * @param shop_id
     * @param product_id
     * @return
     */
    @Update("update oms_cart_item set quantity = quantity+1 where shop_id = #{shop_id} and product_id=#{product_id} and member_id = #{member_id} ")
    int updateNum(@Param("member_id") long member_id,@Param("shop_id") long shop_id,@Param("product_id") long product_id);

    /**
     * 减购物车数量
     * @param cart
     * @return
     */
   @Update("<script>" +
           "update oms_cart_item " +
           "<set> " +
           "<if test=\"quantity == 0 \">" +
           "delete_status = 1,quantity = 0" +
           "</if>" +
           "<if test=\"quantity>0\">" +
           "quantity=quantity - 1" +
           "</if>" +
           "</set>" +
           "where shop_id = #{shop_id} and product_id=#{product.product_id} and member_id = #{member_id}" +
           "</script>")
    int subtractCart(CartList cart);

}
